﻿Imports System.Data

Public Class FormServiciosListado

    Dim classController As New Class_RC_Controller.rc_Servicios()

    Private m_frmServicio As FrmServicios
    Public Property frmServicios() As FrmServicios
        Get
            Return m_frmServicio
        End Get
        Set(value As FrmServicios)
            m_frmServicio = value
        End Set
    End Property

    Private Sub FormServiciosListado_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgServicios.AutoGenerateColumns = False
        CargarCombos()
        CargarServicios()
    End Sub

    Private Sub CargarServicios()

        Dim codTipoServicio As Integer
        If cbxTipoServicio.Checked Then
            codTipoServicio = cboTipoServicio.SelectedValue
        End If

        Dim dt As New DataTable
        dt = classController.get_Servicios_All(oiml.dbConn, 0, txtDescripcion.Text, codTipoServicio, False)
        dgServicios.DataSource = dt

    End Sub

    Private Sub CargarCombos()
        Dim dsTipo As New DataTable
        dsTipo = classController.get_tipo_Servicios(oiml.dbConn)
        If Not dsTipo Is Nothing Then
            If dsTipo.Rows.Count > 0 Then
                cboTipoServicio.DataSource = dsTipo
                cboTipoServicio.DisplayMember = "DescTipoServicio"
                cboTipoServicio.ValueMember = "codTipoServicio"
            End If
        End If
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(sender As System.Object, e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgServicios.SelectedRows.Count > 0 Then
                Dim codServicio As New Integer
                codServicio = dgServicios.SelectedRows(0).Cells(0).Value

                If frmServicios Is Nothing Then
                    frmServicios = New FrmServicios()
                    frmServicios.codServicio = codServicio
                    'If frmServicios.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmServicios.ShowDialog()
                    CargarServicios()
                    frmServicios.Dispose()
                    frmServicios = Nothing
                    'End If
                Else
                    frmServicios.codServicio = codServicio
                    frmServicios.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarServicios()

    End Sub

    Private Sub txtDescripcion_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtDescripcion.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarServicios()

        End If
    End Sub

    Private Sub dgServicios_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgServicios.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgServicios_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgServicios.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub cbxTipoServicio_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxTipoServicio.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarServicios()
        End If
    End Sub

    Private Sub cboTipoServicio_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboTipoServicio.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarServicios()
        End If
    End Sub
End Class